/*******************************************************************************
MapKit.cpp

	Author:		LiuDingXi
	e-mail:		liudx@gmail.com
	Date time:	2009-08-07

	Copyright (C) 2009
	All rights reserved.
	
*******************************************************************************/
#include "StdAfx.h"
#include "MapKit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CMapKit::CMapKit()
{
}

CMapKit::~CMapKit()
{
	Free();
}


BOOL CMapKit::Free()
{
	list<TMAP*>::iterator lit;
	lit = m_MapList.begin();
	while(lit != m_MapList.end())
	{
		TMAP* pMap = *lit++;

		list<TPOS*>::iterator pit;
		pit = pMap->PosList.begin();
		while(pit != pMap->PosList.end())
		{
			TPOS* pPos = *pit++;
			delete pPos;
		}

		list<TIMG*>::iterator iit;
		iit = pMap->ImgList.begin();
		while(iit != pMap->ImgList.end())
		{
			TIMG* pImg = *iit++;
			delete pImg;
		}

		list<TILE*>::iterator eit;
		eit = pMap->TileList.begin();
		while(eit != pMap->TileList.end())
		{
			TILE* pTile = *eit++;
			delete pTile;
		}

		delete pMap;
	}
	m_MapList.clear();

	return TRUE;
}

BOOL CMapKit::Load(CArchive& ar)
{
	TCHAR szFolder[MAX_PATH];
	 _tcscpy(szFolder, ar.GetFile()->GetFilePath());
	 LPTSTR pTail = PathFindFileName(szFolder);
	 if(pTail)
	 {
		 *pTail = 0;
	 }

	TMAP* pMap = NULL;

	CString s;
	while(ar.ReadString(s))
	{
		if(s.IsEmpty())
			continue;
		
		LPCTSTR p = s;
		MoveToBegin(p);
		
		#define IS_TAG(tag)	_tcsnicmp(p, tag, _tcslen(tag)) == 0

		if(IS_TAG(_T("map")))
		{
			pMap = AddMap(p + 3, szFolder);
		}
		else if(IS_TAG(_T("img")))
		{
			if(pMap != NULL)
			{
				pMap->AddImg(p + 3, szFolder);
			}
		}
		else if(IS_TAG(_T("tile")))
		{
			if(pMap != NULL)
			{
				pMap->AddTile(p + 4);
			}
		}
		else if(IS_TAG(_T("pos")))
		{
			if(pMap != NULL)
			{
				pMap->AddPos(p + 3);
			}
		}
	}	
	return TRUE;
}


CMapKit::TMAP* CMapKit::AddMap(LPCTSTR p, LPCTSTR szFolder)
{
	TMAP* pMap = new TMAP;

	pMap->szName = ParseOf(p);
	pMap->szType = ParseOf(p);


	m_MapList.push_back(pMap);

	return pMap;
}


CMapKit::TMAP* CMapKit::FindMap(LPCTSTR szName)
{
	list<TMAP*>::iterator it;
	it = m_MapList.begin();
	while(it != m_MapList.end())
	{
		TMAP* pMap = *it++;
		if(pMap->szName == szName)
		{
			return pMap;
		}
	}
	return NULL;
}




CMapKit::TMAP::TMAP()
{
}

CMapKit::TMAP::~TMAP()
{
}

void CMapKit::TMAP::AddPos(LPCTSTR p)
{
	TPOS* pPos = new TPOS;
	pPos->szName = ParseOf(p);
	pPos->x = ParseOf(p, 0);
	pPos->y = ParseOf(p, 0);
	pPos->cx = ParseOf(p, 0);
	pPos->cy = ParseOf(p, 0);

	PosList.push_back(pPos);
}


void CMapKit::TMAP::AddImg(LPCTSTR p, LPCTSTR szFolder)
{
	TIMG* pImg = new TIMG;
	pImg->szName = ParseOf(p);
	pImg->szFile = ParseOf(p);

	TCHAR szImage[MAX_PATH];
	_tcscpy(szImage, szFolder);
	PathAppend(szImage, pImg->szFile);
	if(pImg->xImage.Load(szImage) == false)
	{
		ASSERT(FALSE);
	}
	
	pImg->x = ParseOf(p, 0);
	pImg->y = ParseOf(p, 0);
	pImg->cx = ParseOf(p, 0);
	pImg->cy = ParseOf(p, 0);

	if(pImg->cx == 0)
	{
		pImg->cx = pImg->xImage.GetWidth();
	}
	if(pImg->cy == 0)
	{
		pImg->cy = pImg->xImage.GetHeight();
	}

	ImgList.push_back(pImg);
}

void CMapKit::TMAP::AddTile(LPCTSTR p)
{
	TILE* pTile = new TILE;
	
	pTile->pImage = NULL;

	CString szImg = ParseOf(p);

	list<TIMG*>::iterator it;
	it = ImgList.begin();
	while(it != ImgList.end())
	{
		TIMG* pImg = *it++;
		if(pImg->szName == szImg)
		{
			pTile->pImage = pImg;
			break;
		}
	}

	pTile->x = ParseOf(p, 0);
	pTile->y = ParseOf(p, 0);

	TileList.push_back(pTile);
}


void CMapKit::TMAP::OnDraw(CDC* pDC, int x, int y)
{
	list<TILE*>::iterator it;
	it = TileList.begin();
	while(it != TileList.end())
	{
		TILE* pTile = *it++;
		if(pTile->pImage)
		{
			pTile->pImage->xImage.Draw(pDC->GetSafeHdc(), x+pTile->x, y+pTile->y);
		}
	}
}
